package com.ct.ah.lab.service.impl;

import com.alibaba.fastjson.JSON;
import com.ct.ah.lab.dao.CashDao;
import com.ct.ah.lab.dao.CashItemDao;
import com.ct.ah.lab.dao.OrderDao;
import com.ct.ah.lab.dao.UserDao;
import com.ct.ah.lab.entity.Cash;
import com.ct.ah.lab.entity.CashItem;
import com.ct.ah.lab.entity.Order;
import com.ct.ah.lab.entity.User;
import com.ct.ah.lab.entity.vo.CashItemVo;
import com.ct.ah.lab.service.ICashService;
import com.ct.ah.lab.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;

@Service("cashService")
public class CashServiceImpl implements ICashService {

    @Autowired
    private CashDao cashDao;
    @Autowired
    private CashItemDao cashItemDao;
    @Autowired
    private UserDao userDao;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private IOrderService orderService;

    @Override
    public Cash getCashByUid(Integer uid) {
        return cashDao.getByUid(uid);
    }

    @Override
    public List<CashItemVo> listCashItem(User user) {
        //格式化时间工具
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        //获取该用户的现金操作记录
        List<CashItem> cashItemList = cashItemDao.getByUid(user.getUid());
        //转换为vo
        //将每个积分记录与产生的订单信息关联起来
        List<CashItemVo> cashItemVoList = JSON.parseArray(JSON.toJSONString(cashItemList), CashItemVo.class);
        for (CashItemVo cashItemVo :
                cashItemVoList) {
            cashItemVo.setPayTime(sdf.format(cashItemVo.getCreatedTime()));
            if (cashItemVo.getOid() != null) {
                Order order = orderService.getbyOid(cashItemVo.getOid());
                cashItemVo.setCashItemOrder(order);
            }


            cashItemVo.setUser(user);
        }
        return cashItemVoList;
    }

    //充值账户
    @Override
    public void chargeCash(User user, Double cashMoney, Integer chargeStyle) {
        //首先判断该用户有没有充值账户，没有则创建，有则修改，同时在充值记录表中生成一条充值记录
        Integer countByUid = cashDao.getCountByUid(user.getUid());
        double remainCash = 0;

        if (countByUid > 0) {
            //当前已存在账户，对账户进行更新
            Cash beforeCash = cashDao.getByUid(user.getUid());
            remainCash = beforeCash.getAccount() + cashMoney;
            beforeCash.setAccount(remainCash);
            cashDao.updateUserCash(beforeCash);
        } else {
            //如果第一次充值，则先生成账户
            Cash cash = new Cash();
            cash.setUid(user.getUid());
            remainCash = cashMoney;
            cash.setAccount(remainCash);
            cashDao.save(cash);
        }

        //生成一条充值记录
        CashItem cashItem = new CashItem();
        cashItem.setUid(user.getUid());
        cashItem.setChangeCash(cashMoney);
        cashItem.setRemainCash(remainCash);
        cashItem.setChargeStyle(chargeStyle);
        cashItem.setProduceReason(2);
        cashItem.setCreatedUser(user.getUsername());

        //插入数据库
        cashItemDao.save(cashItem);
    }

    @Override
    public void cashTransfer(User user, String inUsername, Double outMoney) {
        //首先获取转出账户信息前端进行了把控，说明是金额满足转账需求的，变更转账账户
        Cash outCash = cashDao.getByUid(user.getUid());
        double outUserRemainCash = outCash.getAccount() - outMoney;
        outCash.setAccount(outUserRemainCash);
        cashDao.updateUserCash(outCash);
        //生产转账记录
        //生成一条充值记录
        CashItem outCashItem = new CashItem();
        outCashItem.setUid(user.getUid());
        outCashItem.setChangeCash(outMoney);
        outCashItem.setRemainCash(outUserRemainCash);
        outCashItem.setProduceReason(4);//转账消费
        outCashItem.setCreatedUser(user.getUsername());
        outCashItem.setCname(inUsername);//目标账户
        //插入数据库
        cashItemDao.save(outCashItem);


        //转入账户操作
        User inUser = userDao.login(inUsername);
        //转入账户数据新增
        Integer countByUid = cashDao.getCountByUid(inUser.getUid());
        double inUserRemainCash = 0;

        if (countByUid > 0) {
            //当前已存在账户，对账户进行更新
            Cash beforeCash = cashDao.getByUid(inUser.getUid());
            inUserRemainCash = beforeCash.getAccount() + outMoney;
            beforeCash.setAccount(inUserRemainCash);
            cashDao.updateUserCash(beforeCash);
        } else {
            //如果第一次充值，则先生成账户
            Cash cash = new Cash();
            cash.setUid(inUser.getUid());
            inUserRemainCash = outMoney;
            cash.setAccount(inUserRemainCash);
            cashDao.save(cash);
        }

        //生成转出记录
        CashItem inCashItem = new CashItem();
        inCashItem.setUid(inUser.getUid());
        inCashItem.setChangeCash(outMoney);
        inCashItem.setRemainCash(inUserRemainCash);
        inCashItem.setProduceReason(3);
        inCashItem.setCname(user.getUsername());
        inCashItem.setCid(user.getUid());
        inCashItem.setCreatedUser(inUser.getUsername());

        //插入数据库
        cashItemDao.save(inCashItem);
    }

    @Override
    public void payByCash(Integer oid, User user) {
        Order order = orderDao.getByOid(oid);
        Double consumeMoney = order.getPrice();
        //根据当前用户id获取用户金额
        Cash cash = cashDao.getByUid(user.getUid());
        //变更用户积分
        cash.setAccount(cash.getAccount() - consumeMoney);
        cashDao.updateById(cash);
        //生成消费记录
        CashItem cashItem = new CashItem();
        cashItem.setUid(order.getUid());
        cashItem.setCname(user.getUsername());
        cashItem.setOid(order.getId());
        cashItem.setOrderCode(order.getOrderCode());
        cashItem.setChangeCash(order.getPrice());
        cashItem.setRemainCash(cashDao.getByUid(order.getUid()).getAccount());
        cashItem.setProduceReason(1);
        cashItem.setCreatedUser(order.getCreatedUser());
        cashItemDao.save(cashItem);

        //修改订单状态--待发货---3是钱包现金支付,状态改成2收发货
        orderService.changeStatus(oid, 2, 3, user);
    }

    @Override
    public void returnByCash(Integer oid, User user) {

        //首先判断该用户有没有充值账户，没有则创建，有则修改，同时在充值记录表中生成一条充值记录
        Integer countByUid = cashDao.getCountByUid(user.getUid());
        double remainCash = 0;
        Order order = orderDao.getByOid(oid);
        Double consumeMoney = order.getPrice();

        if (countByUid > 0) {
            //根据当前用户id获取用户金额
            Cash cash = cashDao.getByUid(user.getUid());
            //变更用户积分
            cash.setAccount(cash.getAccount() + consumeMoney);
            cashDao.updateById(cash);
        } else {
            //如果第一次充值，则先生成账户
            Cash cash = new Cash();
            cash.setUid(user.getUid());
            remainCash = consumeMoney;
            cash.setAccount(remainCash);
            cashDao.save(cash);
        }

        //生成退货消费记录
        CashItem cashItem = new CashItem();
        cashItem.setUid(order.getUid());
        cashItem.setOid(order.getId());
        cashItem.setCname(user.getUsername());
        cashItem.setChangeCash(order.getPrice());
        cashItem.setRemainCash(cashDao.getByUid(order.getUid()).getAccount());
        cashItem.setProduceReason(5);
        cashItem.setChargeStyle(2);
        cashItem.setOrderCode(order.getOrderCode());
        cashItem.setCreatedUser(order.getCreatedUser());
        cashItemDao.save(cashItem);

        //修改订单状态--待发货---3是钱包现金支付,状态改成2收发货
        orderService.changeStatus(oid, 4, 4, user);
    }
}
